package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.domain.entity.SkillVersionDO;
import com.xgimi.open.skill.datamanager.domain.mapper.SkillVersionMapper;
import com.xgimi.open.skill.datamanager.domain.service.SkillVersionService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author coderzpw.zhang
 * @description 针对表【skill_version(技能版本表)】的数据库操作Service实现
 * @createDate 2023-02-07 19:23:48
 */
@Service
public class SkillVersionServiceImpl extends ServiceImpl<SkillVersionMapper, SkillVersionDO>
        implements SkillVersionService {

    public static final Integer TEST_STATUS = 0;

    public static final Integer PUBLISHED_STATUS = 1;
    // 技能的测试版本号为0
    public static final Long ZERO = 0L;

    @Override
    public Boolean saveSkillVersion(SkillVersionDO skillVersionDO) {
        return this.save(skillVersionDO);
    }

    @Override
    public SkillVersionDO queryTestVersion(SkillVersionDO skillVersionDO) {
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillVersionDO::getSkillId, skillVersionDO.getSkillId());
        queryWrapper.eq(SkillVersionDO::getStatus, TEST_STATUS);
        queryWrapper.orderByDesc(SkillVersionDO::getVersion);
        // 其实测试版本应该保证只有一个
        return this.list(queryWrapper).get(0);
    }

    @Override
    public List<SkillVersionDO> queryPublishedVersion(SkillVersionDO skillVersionDO) {
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillVersionDO::getSkillId, skillVersionDO.getSkillId());
        queryWrapper.eq(SkillVersionDO::getStatus, PUBLISHED_STATUS);
        queryWrapper.orderByDesc(SkillVersionDO::getVersion);
        return this.list(queryWrapper);
    }

    @Override
    public List<SkillVersionDO> publishedList(Long skillId) {
        LambdaQueryWrapper<SkillVersionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkillVersionDO::getSkillId, skillId);
        wrapper.eq(SkillVersionDO::getStatus, PUBLISHED_STATUS);
        wrapper.orderByDesc(SkillVersionDO::getVersion);
        return this.list(wrapper);
    }

    @Override
    public SkillVersionDO querySkillVersionById(Long id) {
        return this.getById(id);
    }

    @Override
    public SkillVersionDO getTestSkillVersion(Long skillId) {
        LambdaQueryWrapper<SkillVersionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkillVersionDO::getSkillId, skillId);
        // 技能的测试版本号为0
        wrapper.eq(SkillVersionDO::getVersion, ZERO);
        SkillVersionDO ret = this.getOne(wrapper);
        return ret;
    }

    @Override
    public SkillVersionDO getPublishedMaxVersion(Long skillId) {
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillVersionDO::getSkillId, skillId);
        queryWrapper.eq(SkillVersionDO::getStatus, PUBLISHED_STATUS);
        queryWrapper.orderByDesc(SkillVersionDO::getVersion);
        return this.list(queryWrapper).get(0);
    }


    @Override
    public List<SkillVersionDO> queryTestVersionsByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(!CollectionUtils.isEmpty(ids), SkillVersionDO::getSkillId, ids);
        queryWrapper.eq(SkillVersionDO::getVersion, ZERO);
        return this.list(queryWrapper);
    }

    @Override
    public List<SkillVersionDO> listBySkillId(Long skillId) {
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillVersionDO::getSkillId, skillId);
        return this.list(queryWrapper);
    }

    @Override
    public IPage<SkillVersionDO> pageQuery(IPage<SkillVersionDO> page, Long skillId) {
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillVersionDO::getSkillId, skillId);
        queryWrapper.orderByAsc(SkillVersionDO::getStatus);
        queryWrapper.orderByDesc(SkillVersionDO::getVersion);
        return this.page(page, queryWrapper);
    }

    @Override
    public SkillVersionDO queryBySkillIdAndVersion(long skillId, long version) {
        LambdaQueryWrapper<SkillVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillVersionDO::getSkillId, skillId);
        queryWrapper.eq(SkillVersionDO::getVersion, version);
        return this.getOne(queryWrapper);
    }

}




